package ink.lovejinhu.leetcode;

/**
 * @author jinhu
 * created 2021-07-30 9:12
 */
public class Solution_0643 {
    public static void main(String[] args) {
        Solution_0643 solution_0643 = new Solution_0643();
        int[] res = {5};
        int k = 1;
        double maxAverage = solution_0643.findMaxAverage(res, k);
    }

    /**
     * @param nums
     * @param k
     * @return
     */
    public double findMaxAverage(int[] nums, int k) {
        int start = 0;
        int end = k - 1;


        if (end <= nums.length - 1) {
            double max = Integer.MIN_VALUE;
            for (; end < nums.length; end++, start++) {
                double temp01 = this.getAvarage(nums, start, end);

                max = Math.max(temp01, max);
            }
            return max;
        } else {
            double max = 0;
            for (int i = 0; i < nums.length; i++) {
                max += nums[i];
            }
            return max / nums.length;
        }


    }

    public double getAvarage(int[] nums, int start, int end) {
        double res = 0;
        for (int i = start; i <= end; i++) {
            res += nums[i];
        }
        return res / (end - start + 1);
    }
}
